package com.demo.controller;

import com.demo.pojo.Zc;
import com.demo.pojo.Zc;
import com.demo.service.ZcService;
import com.demo.utils.Page;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author： Administrator
 * @date: 21/05/13 14:46
 * 描述：
 */
@Controller
@RequestMapping("/zc")
public class ZcController {
    @Autowired
    private ZcService zcService;

    @RequestMapping("/page.do")
    @ResponseBody
    public Map<String, Object> page(
            @RequestParam(value = "zcName", required = false) String zcName,
            @RequestParam(value = "zcState", required = false) String zcState,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "0") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("message", "");
        Zc zc = new Zc();
        zc.setZcName("%" + zcName + "%");
        zc.setZcState("".equals(zcState) ? null : zcState);
        Page<Zc> page = zcService.page(zc, pageNo, pageSize);
        map.put("count", page.getPageTotalCount());
        map.put("data", page.getItems());
        return map;
    }

    @PostMapping("/add.do")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "name") String zcName) {
        Map<String, Object> map = new HashMap<>();
        int add;
        Zc zc = new Zc();
        zc.setZcName(zcName);
        try {
            add = zcService.add(zc);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "添加资产时服务器发生异常！");
            return map;
        }
        if (add > 0) {
            map.put("code", 0);
            map.put("message", "添加资产成功");
            return map;
        }
        map.put("code", 1);
        map.put("message", "添加资产失败");
        return map;
    }

    @PostMapping("/update.do")
    @ResponseBody
    public Map<String, Object> update(
            @RequestParam(value = "id") Integer zcId,
            @RequestParam(value = "name") String zcName) {
        Map<String, Object> map = new HashMap<>();
        int update;
        Zc zc = new Zc();
        zc.setZcId(zcId);
        zc.setZcName(zcName);
        try {
            update = zcService.update(zc);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "服务器发生异常！");
            return map;
        }
        if (update > 0) {
            map.put("code", 0);
            map.put("message", "修改成功");
            return map;
        }
        map.put("code", 1);
        map.put("message", "修改失败");
        return map;
    }

    @PostMapping("/delete.do")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id") Integer zcId) {
        Map<String, Object> map = new HashMap<>();
        int del;
        try {
            del = zcService.del(zcId);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "修改资产时服务器发生异常！");
            return map;
        }
        if (del > 0) {
            map.put("code", 0);
            map.put("message", "删除资产成功");
            return map;
        }
        if (del == -1) {
            map.put("code", 3);
            map.put("message", "删除资产失败，原因：资产正在借出中");
            return map;
        }
        if (del == -2) {
            map.put("code", 2);
            map.put("message", "删除资产失败，原因：资产正在维修中");
            return map;
        }
        map.put("code", 1);
        map.put("message", "删除资产失败");
        return map;
    }
    @RequestMapping("/overPage.do")
    @ResponseBody
    public Map<String, Object> overPage(
            @RequestParam(value = "zcName", required = false) String zcName,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "0") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("message", "");
        Zc zc = new Zc();
        zc.setZcName("%" + zcName + "%");
        zc.setZcState("正常");
        Page<Zc> page = zcService.page(zc, pageNo, pageSize);
        map.put("count", page.getPageTotalCount());
        map.put("data", page.getItems());
        return map;
    }
}
